package face.pay.web.com.login;

import face.pay.system.SessionUtils;
import face.pay.system.annotation.CurrentUser;
import face.pay.common.user.service.UserRegisterBiz;
import face.pay.constant.UserType;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import face.pay.web.com.login.servcie.IloginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class SystemController {


    @Autowired
    private IloginService iloginService;

    @Autowired
    private UserRegisterBiz userRegisterBiz;


    @RequestMapping(value = "/")
    public String app(@CurrentUser  UserInfo loginUser){

        String userType = loginUser.getType();
        if(UserType.ENTERPRISE.equals(userType)){
            return "agency/index";
        }else{
            return "agency/index";
        }
    }

    @RequestMapping(value = "/index")
    public String index(@CurrentUser  UserInfo loginUser){

        String userType = loginUser.getType();
        if(UserType.ENTERPRISE.equals(userType)){
            return "agency/index";
        }else{
            return "agency/index";
        }
    }

    @RequestMapping(value="/user_center")
    public String userCenter(@CurrentUser UserInfo loginUser){

           String userType = loginUser.getType();

            return "com/center/userCenter";


    }

    @RequestMapping(value = "/login")
    public String login(){
        return  "login";
    }



    @RequestMapping(value = "/log_out")
    public String  logOut(HttpServletRequest request, HttpServletResponse response){
        SessionUtils.removeUserFromSession(request);

        return "redirect:/login";
    }


    @RequestMapping(value = "/do_login")
    @ResponseBody
    public ResultMessage do_login(HttpServletRequest request,UserInfo user,String domain){

        if(StringUtils.isNotBlank(user.getMobile()) && StringUtils.isNotBlank(user.getPassWord() )){
            UserInfo userInfo = this.iloginService.login(user, domain);
            if(userInfo!=null){
                SessionUtils.addUserToSession(request,userInfo);

                return  ResultMessage.createSuccess();
            }else {
                return  ResultMessage.createFail("手机号或者密码错误");
            }
        }else {
            return  ResultMessage.createFail("手机号或者密码错误");
        }

    }




}

